package b2.config
{
	public class CampConfig
	{
		//索引
		public var index:int;
		//类型（0, 普通战役; 1, 对战战役; 2, 比赛战役）
		public var type:int;
		//战役名称
		public var name:String;
		//战役详情
		public var description:String;
		//初始生命数
		public var lifeNum:int;
		//难度
		public var difficulty:int;
		//推荐人数
		public var recommendedPlayer:int;
		//比赛信息
		public var matchConfigs:Array;
		//关卡列表
		public var battleIndex:Array;
		//奖励列表
		public var awards:Array;
		//需要等级 
		public var requireLevel:int;
		//要求完成的任务索引
		public var requireTaskIndex:int;
		
		private const STAR:Array = ["★", "●", "▲"];
		
		public function CampConfig(campXML:XML)
		{
			index = campXML.index;
			type = campXML.type;
			name = campXML.name;
			description = campXML.description;
			lifeNum = campXML.lifeNum;
			difficulty = campXML.difficulty;
			recommendedPlayer = campXML.recommendedPlayer;
			if (campXML.battleIndex != undefined)
				battleIndex = campXML.battleIndex.split(",");
			requireLevel = campXML.requireLevel;
			requireTaskIndex = campXML.requireTaskIndex;
			//解析比赛信息
			matchConfigs = new Array();
			for (var i:int = 0; i < campXML.match.length(); i++)
			{
				matchConfigs.push(new MatchConfig(campXML.match[i]));
			}
			awards = new Array();
			for (i = 0; i < campXML.award.length(); i++)
			{
				var award:CampAwardConfig = new CampAwardConfig(campXML.award[i]);
				awards.push(award);
			}
		}
		
		//获得敌人信息
		public function getEnemy():Array
		{
			//敌人信息
			var enemies:Object = new Object();
			var enemyArr:Array = new Array();
			for (var i:int = 0; i < battleIndex.length; i++)
			{
				var battleConfig:BattleConfig = Config.battleConfigs[battleIndex[i]];
				if (battleConfig.enemyAppears != null)
				{
					for (var j:int = 0; j < battleConfig.enemyAppears.length; j++)
					{
						var enemyAppearStrs:Array = battleConfig.enemyAppears[j].split(","); 
						var enemyIndex:int = enemyAppearStrs[0];
						var enemyLevel:int = enemyAppearStrs[2];
						var bossLevel:int = enemyAppearStrs.length > 6 ? enemyAppearStrs[6] : -1;
						if (enemies[enemyIndex] == null || enemyLevel >= enemies[enemyIndex][0] && bossLevel >= enemies[enemyIndex][1])
						{
							enemies[enemyIndex] = [enemyLevel, bossLevel];
						}
					}
				}
			}
			for (var enemyIndexStr:String in enemies)
			{
				enemyArr.push([parseInt(enemyIndexStr), enemies[enemyIndexStr][0], enemies[enemyIndexStr][1]]);
			}
			enemyArr.sort(enemySort);
			return enemyArr;
		}
		
		public function getDiffStar():String
		{
			var diff:int = difficulty;
			var count:int = 0;
			var diffStar:String = "";
			while (diff > 0)
			{
				for (var i:int = 0; i < diff % 5; i++)
					diffStar = STAR[count] + diffStar;
				diff = diff / 5;
				count++;
			}
			return diffStar;
		}
		
		private function enemySort(a:Array, b:Array):int
		{
			if (a[2] < b[2])
				return 1;
			else if (a[2] > b[2])
				return -1;
			if (a[1] < b[1])
				return 1;
			else if (a[1] > b[1])
				return -1;
			else
				return 0; 
		}

	}
}